package com.badibadi.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.badibadi.activity.AddApplyFormActivity;
import com.badibadi.activity.ClubHomepageActivity;
import com.badibadi.activity.MyDataActivity;
import com.badibadi.activity.PinyinSeeOtherPeopleFriendActivity;
import com.badibadi.activity.SeeOtherPeopleSpaceActivity;
import com.badibadi.infos.ClubApplyModel;
import com.badibadi.infos.Invite_Friend_Model;
import com.badibadi.infos.Results;
import com.badibadi.mytools.CalendarTools;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.view.my_view.XListView;
import com.view.my_view.XListView.IXListViewListener;
//club_invite_img_common
public class AfterLandingAddFriendsMsgItemFragment1 extends BaseAppFragment implements
		IXListViewListener {
	private XListView2Adapter___1 x2Adapter;
	private XListView xListView2;
	private int Page;
	private Results results;
	private List<ClubApplyModel> friend_Models, list;

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(getActivity());
				
				try {
					onLoad();
					Utils.showMessage(getActivity(), getResources().getString(R.string.l_net_error));
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;
			case 2:
				try {
					Utils.ExitPrgress(getActivity());
					friend_Models.addAll(list);
					x2Adapter.notifyDataSetChanged();
					onLoad();
				} catch (Exception e) {
					// TODO: handle exception
				}
			
				break;
			case 3:
				Utils.ExitPrgress(getActivity());
			
				try {
					onLoad();
					Utils.showMessage(getActivity(), getResources().getString(R.string.l_xa10));
				} catch (Exception e) {
					// TODO: handle exception
				}

				break;

			default:
				break;
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		friend_Models = new ArrayList<ClubApplyModel>();
		x2Adapter = new XListView2Adapter___1();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.after_landing_freinds00_1, null);
		InitMyXListView2(view);
		return view;
	}

	public void InitMyXListView2(View view) {
		Page = 0;
		Constants.Invite_handler = mHandler;
		xListView2 = (XListView) view
				.findViewById(R.id.after_landing_friends00_xlistView);
		xListView2.setXListViewListener(this);
		xListView2.setPullLoadEnable(true);
		xListView2.setPullRefreshEnable(true);
		xListView2.setAdapter(x2Adapter);
		xListView2.startLoadMore();
	}

	private void Invite_announce(final int page) {
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				list = new ArrayList<ClubApplyModel>();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", Utils.getUid(getActivity()));
				map.put("type", 2);
				map.put("page", page);
				map.put("pageNum", 10);
				String invite_content = Utils.sendRequest(map,
						Constants.BadiUrl + "invite_announce");
				if (invite_content != null) {
					results = Utils.checkResult_NNN(getActivity(),
							invite_content);
					if (results != null && !results.getRetmsg().equals("null")) {
//						   ("解析出来的数据:" + results.getRetmsg());
						try {
							list = JSONUtils.getListByJsonString(
									results.getRetmsg(), ClubApplyModel.class);
							mHandler.sendEmptyMessage(2);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							   
						}
					} else {
						if (page > 1) {
							--Page;
						} else {
							Page = 1;
						}
						mHandler.sendEmptyMessage(3);
					}
				} else {
					if (page > 1) {
						--Page;
					} else {
						Page = 1;
					}
					mHandler.sendEmptyMessage(1);
				}
			}
		});
	}

	protected void onLoad() {
		xListView2.stopRefresh();
		xListView2.stopLoadMore();
		xListView2.setRefreshTime(""+getResources().getString(R.string.ganggang));
	}

	class XListView2Adapter___1 extends BaseAdapter {

		private DisplayImageOptions options;
		private Results resultsdd;

		private Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case 1:
					try {
						Utils.ExitPrgress(getActivity());
						Utils.showMessage(getActivity(), getResources().getString(R.string.l_net_error));
					} catch (Exception e) {
						// TODO: handle exception
					}
					break;
				case 4:
					try {
						Utils.ExitPrgress(getActivity());
						Utils.showMessage(getActivity(), getResources().getString(R.string.l_xa10));
					} catch (Exception e) {
						// TODO: handle exception
					}
					break;
				case 5:
					try {
						Utils.ExitPrgress(getActivity());
						Utils.showMessage(getActivity(), resultsdd.getRetmsg());
					} catch (Exception e) {
						// TODO: handle exception
					}
					break;
				case 6:
					try {
						Utils.ExitPrgress(getActivity());
						Utils.showMessage(getActivity(), getResources().getString(R.string.wx_add_success));
					} catch (Exception e) {
						// TODO: handle exception
					}
					break;
				case 7:
					try {
						Utils.ExitPrgress(getActivity());
						Utils.showMessage(getActivity(), getResources().getString(R.string.wx_txt_51));
					} catch (Exception e) {
						// TODO: handle exception
					}
					break;
				case 8:
					try {
						Utils.ExitPrgress(getActivity());
						Utils.showMessage(getActivity(), getResources().getString(R.string.wx_txt_51));
					} catch (Exception e) {
						// TODO: handle exception
					}
					break;
				default:
					break;
				}
			}
		};

		public XListView2Adapter___1() {
			options = new DisplayImageOptions.Builder()
					.showStubImage(R.drawable.ic_stub)
					.showImageForEmptyUri(R.drawable.noimage)
					.showImageOnFail(R.drawable.noimage).cacheInMemory()
					.cacheOnDisc().displayer(new RoundedBitmapDisplayer(5))
					.build();
		}

		@Override
		public int getCount() {
			return friend_Models.size();
		}

		@Override
		public Object getItem(int position) {
			return friend_Models.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}
//club_invite_img_name club_invite_img_type club_invite_img_time
		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			LayoutInflater inflater = LayoutInflater.from(getActivity());
			convertView = inflater.inflate(R.layout.club_invitation, null);
			ImageView club_invite_img_back = (ImageView) convertView
					.findViewById(R.id.club_invite_img_back);
			final ImageView club_invite_img_no_add = (ImageView) convertView
					.findViewById(R.id.club_invite_img_no_add);
			final ImageView club_invite_img_add = (ImageView) convertView
					.findViewById(R.id.club_invite_img_add);
			TextView club_invite_img_name = (TextView) convertView
					.findViewById(R.id.club_invite_img_name);
			TextView club_invite_img_type = (TextView) convertView
					.findViewById(R.id.club_invite_img_type);
			TextView club_invite_img_common = (TextView) convertView
					.findViewById(R.id.club_invite_img_common);
			TextView club_invite_img_time = (TextView) convertView
					.findViewById(R.id.club_invite_img_time);
			TextView see=(TextView)convertView.findViewById(R.id.see);
			club_invite_img_name.setText(friend_Models.get(position)
					.getNickName());
			club_invite_img_name.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
//					Intent intent=new Intent(getActivity(),SeeOtherPeopleSpaceActivity.class);
//					intent.putExtra("friend_id",friend_Models.get(position).getUid());
//					startActivity(intent);
					if(friend_Models.get(position).getUid()!=null)
					{
						if(!friend_Models.get(position).getUid().equals(""+Utils.getUid(getActivity())))
						{
							Intent intent=new Intent(getActivity(),SeeOtherPeopleSpaceActivity.class);
							intent.putExtra("friend_id", friend_Models.get(position).getUid());
							startActivity(intent);
						}else
						{
							Intent intent=new Intent(getActivity(),MyDataActivity.class);
//							intent.putExtra("friend_id", friend_Models.get(position).getId());
							startActivity(intent);
						}
					}
				}
			});
			club_invite_img_type.setText(friend_Models.get(position).getName());
			club_invite_img_type.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent=new Intent(getActivity(),ClubHomepageActivity.class);
					intent.putExtra("cid", friend_Models.get(position).getCid());
					startActivity(intent);
				}
			});
			see.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent=new Intent(getActivity(),ClubHomepageActivity.class);
					intent.putExtra("cid", friend_Models.get(position).getCid());
					startActivity(intent);
				}
			});
			club_invite_img_common.setText(friend_Models.get(position)
					.getEqual());
			club_invite_img_common.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent =new Intent(getActivity(),PinyinSeeOtherPeopleFriendActivity.class);
					intent.putExtra("uid", friend_Models.get(position).getUid());
					intent.putExtra("fid", Utils.getUid(getActivity()));
					startActivity(intent);
				}
			});
			club_invite_img_time.setText(CalendarTools.getTimeChangeToString(getActivity(),friend_Models.get(position).getTime()));
			ImageLoader.getInstance().displayImage(
					Constants.BadiDownImgUrl
							+ friend_Models.get(position).getHead()+Constants.appPhoto4img,
					club_invite_img_back, options);
			
			club_invite_img_back.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent=new Intent(getActivity(),SeeOtherPeopleSpaceActivity.class);
					intent.putExtra("friend_id",friend_Models.get(position).getUid());
					startActivity(intent);
				}
			});
			
			
			
			
			club_invite_img_add.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
//					club_invite_img_no_add.setVisibility(View.GONE);
//					Toast.makeText(getActivity(), "加入俱乐部", 0).show();
					if (friend_Models.get(position).getIs_apply().equals("0")) {
						Add_club(list.get(position).getCid(), "0", "");
					} else {
						Intent intent = new Intent(getActivity(),
								AddApplyFormActivity.class);
						intent.putExtra("pan", "set");
						intent.putExtra("cid", friend_Models.get(position)
								.getCid());
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						getActivity().startActivity(intent);
					}
				}
			});
			club_invite_img_no_add.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
//					club_invite_img_add.setVisibility(View.GONE);
					Toast.makeText(getActivity(), getResources().getString(R.string.wx_refuse_add_club), 0).show();
				}
			});

			return convertView;
		}

		private void Add_club(final String cid, final String is_apply,
				final String content) {
			// try {
			// Utils.showPrgress(getActivity());
			// } catch (Exception e) {
			// // TODO: handle exception
			// }
			MyThreadTool.fixedThreadPool.execute(new Runnable() {
				@Override
				public void run() {
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("uid", Utils.getUid(getActivity()));
					map.put("cid", cid);
					map.put("is_apply", is_apply);
					if (is_apply.equals("1")) {
						map.put("contents", content);
					}
//					   
					String content = Utils.sendRequest(map, Constants.BadiUrl_1
							+ "club/attendClub");
//					   
					if (content != null) {
						resultsdd = Utils.checkResult_NNN(getActivity(),
								content);
						//    ("加入俱乐部返回的参数是:" + content + " "
						// + resultsdd.getRetmsg());
						if (resultsdd != null
								&& !resultsdd.getRetmsg().equals("null")) {
							
							if(resultsdd.isRet()==true)
							{
								handler.sendEmptyMessage(6);
							}else
							{
								handler.sendEmptyMessage(7);
							}
							
							
						} else {
							handler.sendEmptyMessage(8);
						}
					} else {
						handler.sendEmptyMessage(1);
					}
				}
			});
		}

	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		Page = 1;
		friend_Models = new ArrayList<ClubApplyModel>();
		Invite_announce(Page);
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		Invite_announce(++Page);
	}
}